var _APP_SYSTEM_BASE_DIR_ = null;

(function() {
    //默认字体
    var SM_MEIP_DEFAULT_FONT = "font-3";
    var SM_MEIP_FONT4CACHE_KEY = "SM_MEIP_FONT";
    
    //默认皮肤样式
    var SM_MEIP_DEFAULT_STYLE = "blue_gray";
    var SM_MEIP_STYLE4CACHE_KEY = "SM_MEIP_STYLE";
    
    /**
     * 设置默认字体风格.
     * @param font 系统使用的字体风格
     */
    window["setAppSystemFont"] = function(font) {
        if (font == null || font == "") {
            font = SM_MEIP_DEFAULT_FONT;
        }
        
        window.localStorage[SM_MEIP_FONT4CACHE_KEY]= font;
    }
    
    /**
     * 获取系统使用的字体风格.
     * @param font 系统使用的字体风格
     */
    window["getAppSystemFont"] = function() {
        var font = window.localStorage[SM_MEIP_FONT4CACHE_KEY];
        return (font == null || font == "" ? SM_MEIP_DEFAULT_FONT : font);
    }
    
    /**
     * 设置默认皮肤样式.
     * @param style 系统使用的皮肤样式
     */
    window["setAppSystemStyle"] = function(style) {
        if (style == null) {
            style = SM_MEIP_DEFAULT_STYLE;
        }
        
        window.localStorage[SM_MEIP_STYLE4CACHE_KEY] = style;
    }
    
    /**
     * 获取系统使用的皮肤样式.
     */
    window["getAppSystemStyle"] = function() {
        var style = window.localStorage[SM_MEIP_STYLE4CACHE_KEY];
        return (style == null || style == "" ? SM_MEIP_DEFAULT_STYLE : style);
    }
    
    window["getAppSystemBaseDir"] = function() {
        return (_APP_SYSTEM_BASE_DIR_ == null ? "" : _APP_SYSTEM_BASE_DIR_);
    }
    
    /**
     * 加载JS文件.
     */
    function LoadJs(js, isWLoad) {
        function _lj(url) {
            var script = document.createElement("script");
            script.type = "text/javascript";
            script.src = url;
            document.body.appendChild(script);
        }

        if ( typeof (js) == "string") {
            if (isWLoad) {
                document.writeln("<script src='" + getAppSystemBaseDir() + js + "'><\/script>");
            } else {
                _lj(getAppSystemBaseDir() + js);
            }
        } else {
            for (var i = 0; i < js.length; i++) {
                if (isWLoad) {
                    document.writeln("<script src='" + getAppSystemBaseDir() + js[i] + "'><\/script>");
                } else {
                    _lj(getAppSystemBaseDir() + js[i]);
                }
            }
        }
    };

    /**
     * 加载CSS文件.
     */
    function LoadCss(css, isWLoad, attrs, callbacks) {
        function _lc(url, callback) {
            var link = document.createElement("link");
            link.type = "text/css";
            link.rel = "stylesheet";
            for (var i = 0; i < attrs.length; i++) {
                var attr = attrs[i];
                link.setAttribute(attr.attrName, attr.attrVal);
            }
            
            link.href = url;
            
            if (callback) {
                link.onload = function() {
                    callback.execute();
                }
            }
            
            document.getElementsByTagName("head")[0].appendChild(link);
        }
        
        var appSysBaseDir = getAppSystemBaseDir();
        
        if ( typeof (css) == "string") {
            var loadCssFilePath = "";
            
            if (appSysBaseDir == "") {
                loadCssFilePath = css;
            } else {
                loadCssFilePath = appSysBaseDir + "/" + css;
            }
            
            if (isWLoad) {
                document.writeln("<link rel='stylesheet' type='text/css' href='" + loadCssFilePath + "'\/>");
            } else {
                _lc(loadCssFilePath, callbacks);
            }
        } else {
            for (var i = 0; i < css.length; i++) {
                var loadCssFilePath = "";
                
                if (appSysBaseDir == "") {
                    loadCssFilePath = css[i];
                } else {
                    loadCssFilePath = appSysBaseDir + "/" + css[i];
                }
                
                if (isWLoad) {
                    document.write("<link rel='stylesheet' type='text/css' href='" + loadCssFilePath + "'\/>");
                } else {
                    _lc(loadCssFilePath, callbacks[i]);
                }
            }
        }
    };
    
    /**
     * 加载字体.
     */
    window["LoadCommonFont"] = function(callback) {
        if (callback) {
            try {
                jq(document).find('link[font="true"]').remove();
            } catch (e) { }
        }
        
        var fontName = getAppSystemFont();
        var fontPath = "common/css/font/{font}.css";
        var FONT_SIGN = "\{font\}";
        fontPath = fontPath.replace(FONT_SIGN, fontName);

        LoadCss(fontPath, false, [{
            "attrName" : "font",
            "attrVal" : true
        }], callback);
    };

    /**
     * 加载皮肤样式.
     */
    window["LoadCommonStyle"] = function(callback) {
        if (callback) {
            try {
                jq(document).find('link[skin="true"]').remove();
            } catch (e) { }
        }        //查找已存在的皮肤样式，移除掉
        
        var styleName = getAppSystemStyle();
        var style = "common/css/skin/{styles}/style.css";
        var STYLES_SIGN = "\{styles\}";
        style = style.replace(STYLES_SIGN, styleName);

        LoadCss(style, false, [{
            "attrName" : "skin",
            "attrVal" : true
        }]);
    };
    
    function initSystemBaseDir() {
        var COMMON_FILE_NAME = "supp-common.jsx";
        
        jq('script').each(function() {
            var script = jq(this).attr("src");
            if (script && script.indexOf(COMMON_FILE_NAME) > -1) {
                var FLAG = "..";
                var endInex = script.lastIndexOf(FLAG);
                if (endInex > -1) {
                    _APP_SYSTEM_BASE_DIR_ = script.substring(0, endInex + FLAG.length);
                } else {
                    _APP_SYSTEM_BASE_DIR_ = "";
                }
                return;
            }
        });
    }
    
    //页面初始化
    window["LoadAllCommonFile"] = function() {
        initSystemBaseDir();
        LoadCommonStyle();
        LoadCommonFont();
    };

    LoadAllCommonFile();
})();